package com.wbao.dianniu.viewpager;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;

public class ImageFragment extends Fragment {


    private static final String IMAGE_URL = "image";
    private static final String IMAGE_SIZE = "image_size";
    private static final String IMAGE_INDEX = "image_index";
    PhotoView image;
    private String imageUrl;
    private Dialog dialog;

    public ImageFragment() {
        // Required empty public constructor
    }

    public static ImageFragment newInstance(String param1/*,int size,int currentPage*/) {
        ImageFragment fragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putString(IMAGE_URL, param1);
//        args.putInt(IMAGE_SIZE, size);
//        args.putInt(IMAGE_INDEX, currentPage);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imageUrl = getArguments().getString(IMAGE_URL);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, container, false);
        image = (PhotoView) view.findViewById(R.id.photo_view_image);
		Glide.with(getContext()).load(imageUrl).into(image);
		image.setOnLongClickListener(new MyLongClickListener());
		image.setOnPhotoTapListener(new OnPhotoTapListener() {
			
			@Override
			public void onPhotoTap(View arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				getActivity().finish();
				getActivity().overridePendingTransition(0,R.anim.zoom_exit);  
			}
		});
		
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    
	class MyLongClickListener implements OnLongClickListener {

		@Override
		public boolean onLongClick(View v) {
			showDialog();
			return false;
		}
	}
    
	/**
	 * 显示保存图片弹出框
	 */
	private void showDialog() {
		dialog = new Dialog(getActivity(), R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.BOTTOM);
		View view = LayoutInflater.from(getActivity()).inflate(R.layout.qa_collection_dialog, null);
		Button collectBtn = (Button) view.findViewById(R.id.qa_collection_button);
		Button cancelBtn = (Button) view.findViewById(R.id.qa_collection_cancel);
		collectBtn.setText("保存图片");
		//保存图片
		collectBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				
			}
		});
		// 取消
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
	}
	
}
